﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MINERP.DAL.PrintManage;
using System.Runtime.InteropServices;

namespace MINERP.Frontier
{
    public partial class FrmResult : Form
    {
        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);

        private DataTable _viewDataSource = new DataTable();

        public FrmResult(int boardID)
        {
            DALMain.GetDetailInfoFromBoardID(boardID, out _viewDataSource);
            InitializeComponent();

            this.txtBoardID.KeyPress += new KeyPressEventHandler(ControlInput);
            this.txtPhotoid.KeyPress += new KeyPressEventHandler(ControlInput);
        }

        private void FrmResult_Load(object sender, EventArgs e)
        {
            this.dataGridResult.DataSource = _viewDataSource;
            this.dataGridResult.ClearSelection();
            this.txtBoardID.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int boarid = 0;
            int photoid = 0;
            string ordercode = null;

            if (this.txtBoardID.Text.Trim() != string.Empty)
            {
                boarid = int.Parse(this.txtBoardID.Text.Trim());
            }

            if (this.txtOrderCode.Text.Trim() != string.Empty)
            {
                ordercode = this.txtOrderCode.Text.Trim();
            }

            if (this.txtPhotoid.Text.Trim() != string.Empty)
            {
                photoid = int.Parse(this.txtPhotoid.Text.Trim());
            }

            this._viewDataSource.Clear();

            DALMain.GetDetailInfoFromComboCondition(boarid,ordercode, photoid, out _viewDataSource);
            this.dataGridResult.DataSource = _viewDataSource;
            this.dataGridResult.ClearSelection();
        }

        /// <summary>
        /// 设置颜色
        /// </summary>
        private void SetBackColor()
        { 
    
            for (int i = 0; i < this.dataGridResult.Rows.Count; i++)
            {
                Color dgvColor = dataGridResult.DefaultCellStyle.BackColor;

                switch (dataGridResult.Rows[i].Cells["status"].Value.ToString())
                {
                    case "正在拼版":
                        dgvColor = Color.BlanchedAlmond;
                        break;
                    case "正在打印":
                        dgvColor = Color.Cyan;
                        break;
                    case "已打印":
                        dgvColor = Color.LightCyan;
                        break;
                    case "已分片":
                        dgvColor = Color.DarkCyan;
                        break;
                    default:
                        break;
                }
                dataGridResult.Rows[i].DefaultCellStyle.BackColor = dgvColor;

            }
        }

        /// <summary>
        /// 输入控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ControlInput(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
            {
                ;
            }
            else if (char.IsControl(e.KeyChar))
            {
                ;
            }
            else
            {
                e.KeyChar = (char)Keys.None;
            }
        }

        private void dataGridResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            SetBackColor();
        }

        private void dataGridResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex ==5)
            {
                string fileName = dataGridResult.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (fileName.Contains("jpg"))
                {
                    int ret = WinExec(@"explorer /n,/select," + fileName, 1);
                }
            }
        }

    }
}